﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataLayer.Connection;

using Oracle.DataAccess;
using System.Data.OracleClient;
using EntitiesLayer.Entities;
using System.Data;

namespace DataLayer.DAO
{
    public class ReclamanteDAO
    { 
        private Connection.Connection cn;
        private OracleConnection objCn;

        #region "Procedures and Methods Oracle10g"

        public ReclamanteDAO()
        {
            cn = new Connection.Connection();
            objCn = cn.Conecta();
        }
        #region  "Get Data"
        public List<Reclamante> buscarReclamantes(string apellidorecla, string nombrerecla)
        {
            List<Reclamante> lstObject = new List<Reclamante>();

            try
            {
                OracleCommand cmd = new OracleCommand("BUSCARRECLAMANTECRITERIO", objCn);
                objCn.Open();
                cmd.Connection = objCn;
                cmd.CommandTimeout = 60;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new OracleParameter("RECORDSET", OracleType.Cursor, 100, ParameterDirection.Output, null, DataRowVersion.Current, false, null));
                cmd.Parameters.Add(new OracleParameter("RREC_APELLIDOS_RECL", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, apellidorecla));
                cmd.Parameters.Add(new OracleParameter("RREC_NOMBRES_RECL", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, nombrerecla));

                using (IDataReader oRead = cmd.ExecuteReader(CommandBehavior.SingleResult))
                {
                    if (oRead != null)
                    {
                        while (oRead.Read())
                        {
                            Reclamante oReclamante = new Reclamante();

                            if (!oRead.IsDBNull(oRead.GetOrdinal("IREC_NRO_DOCUMENTO")))
                            {
                                if (!string.IsNullOrEmpty(oRead.GetString(oRead.GetOrdinal("IREC_NRO_DOCUMENTO")).ToString()))
                                {
                                    oReclamante.IREC_NRO_DOCUMENTO = oRead.GetString(oRead.GetOrdinal("IREC_NRO_DOCUMENTO"));
                                }
                            }
                            if (!oRead.IsDBNull(oRead.GetOrdinal("VREC_APELLIDOS_RECL")))
                            {
                                if (!string.IsNullOrEmpty(oRead.GetString(oRead.GetOrdinal("VREC_APELLIDOS_RECL")).ToString()))
                                {
                                    oReclamante.VREC_APELLIDOS_RECL = oRead.GetString(oRead.GetOrdinal("VREC_APELLIDOS_RECL"));
                                }
                            }

                            if (!oRead.IsDBNull(oRead.GetOrdinal("VREC_NOMBRES_RECL")))
                            {
                                if (!string.IsNullOrEmpty(oRead.GetString(oRead.GetOrdinal("VREC_NOMBRES_RECL")).ToString()))
                                {
                                    oReclamante.VREC_NOMBRES_RECL = oRead.GetString(oRead.GetOrdinal("VREC_NOMBRES_RECL"));
                                }
                            }

                            if (!oRead.IsDBNull(oRead.GetOrdinal("VREC_TELEF_FIJO")))
                            {
                                if (!string.IsNullOrEmpty(oRead.GetString(oRead.GetOrdinal("VREC_TELEF_FIJO")).ToString()))
                                {
                                    oReclamante.VREC_TELEF_FIJO = oRead.GetString(oRead.GetOrdinal("VREC_TELEF_FIJO"));
                                }
                            }
                            if (!oRead.IsDBNull(oRead.GetOrdinal("VREC_CELULAR")))
                            {
                                if (!string.IsNullOrEmpty(oRead.GetString(oRead.GetOrdinal("VREC_CELULAR")).ToString()))
                                {
                                    oReclamante.VREC_CELULAR = oRead.GetString(oRead.GetOrdinal("VREC_CELULAR"));
                                }
                            }

                            if (!oRead.IsDBNull(oRead.GetOrdinal("VREC_EMAIL")))
                            {
                                if (!string.IsNullOrEmpty(oRead.GetString(oRead.GetOrdinal("VREC_EMAIL")).ToString()))
                                {
                                    oReclamante.VREC_EMAIL = oRead.GetString(oRead.GetOrdinal("VREC_EMAIL"));
                                }
                            }

                            lstObject.Add(oReclamante);
                            oReclamante = null;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                objCn.Dispose();
            }
            finally
            {

                if (objCn != null)
                {
                    objCn.Close();
                }

            }

            return lstObject;

        }
        public Reclamante getreclamante(int nrodocumento)
        {
            Reclamante p = new Reclamante();

            using (OracleCommand cmd = new OracleCommand("RECUPERARRECLAMANTE", objCn))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                OracleParameter refparameter = new OracleParameter();
                refparameter.ParameterName = "RECORDSET";
                refparameter.OracleType = OracleType.Cursor;
                refparameter.Direction = ParameterDirection.Output;
                OracleParameter idparameter = new OracleParameter();
                idparameter.ParameterName = "ID_RECLA";
                idparameter.OracleType = OracleType.Int32;
                idparameter.Direction = ParameterDirection.Input;
                idparameter.Value = nrodocumento;
                cmd.Parameters.Add(refparameter);
                cmd.Parameters.Add(idparameter);

                try
                {
                    objCn.Open();

                    OracleDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        p.VREC_APELLIDOS_RECL = dr.GetString(dr.GetOrdinal("VUSU_NRO_DOCUMENTO"));
                        p.VREC_NOMBRES_RECL = dr.GetString(dr.GetOrdinal("VREC_NOMBRES_RECL"));
                        p.IREC_NRO_DOCUMENTO = dr.GetString(dr.GetOrdinal("IREC_NRO_DOCUMENTO"));
                        p.VREC_TELEF_FIJO = dr.GetString(dr.GetOrdinal("VREC_TELEF_FIJO"));
                        p.VREC_CELULAR = dr.GetString(dr.GetOrdinal("VREC_CELULAR"));
                        p.VREC_EMAIL = dr.GetString(dr.GetOrdinal("VREC_EMAIL"));
                    }
                }
                catch (Exception ex)
                {
                    objCn.Dispose();
                }
                finally
                {
                    objCn.Close();
                }
            }
            return p;
        }
        #endregion

        #region  "Insert,Update Data"        
        public int insertReclamante(Reclamante obj)
        {
            int resultado = 0;
            try
            {
                OracleCommand cmd = new OracleCommand("INSERTRECLAMANTE", objCn);
                objCn.Open();
                cmd.Connection = objCn;
                cmd.CommandTimeout = 60;
                cmd.CommandType = CommandType.StoredProcedure;
                // Datos del Reclamo 
                cmd.Parameters.Add(new OracleParameter("Rrec_Apellidos_Recl", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VREC_APELLIDOS_RECL));
                cmd.Parameters.Add(new OracleParameter("Rrec_Nombres_Recl", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VREC_NOMBRES_RECL));
                cmd.Parameters.Add(new OracleParameter("Rrec_Nro_Documento", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.IREC_NRO_DOCUMENTO));
                cmd.Parameters.Add(new OracleParameter("Rrec_Telef_Fijo", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VREC_TELEF_FIJO));
                cmd.Parameters.Add(new OracleParameter("Rrec_Celular", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VREC_CELULAR));
                cmd.Parameters.Add(new OracleParameter("Rrec_Email", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VREC_EMAIL));
                cmd.Parameters.Add(new OracleParameter("Rpac_Cod_Rel_Pac", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VPAC_COD_REL_PAC));
                cmd.Parameters.Add(new OracleParameter("Rdoc_Cod_Tip_Doc", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VDOC_COD_TIP_DOC));
                cmd.Parameters.Add(new OracleParameter("Rrec_Pacienteoreprlegal", OracleType.NVarChar, 10, ParameterDirection.Input, false, 10, 0, null, DataRowVersion.Current, obj.VREC_PACIENTEOREPRLEGAL));                           
                resultado = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                objCn.Dispose();
            }
            finally
            {
                if (objCn != null)
                {
                    objCn.Close();
                }
            }
            return resultado;
        }
        #endregion
       
        #endregion

    }
}
